package behavioral.chain_of_responsibility.impl2;

/**
 * @ClassName: HandlerChain
 * @Description: 客户类（Client）角色：创建处理链，并向链头的具体处理者对象提交请求，它不关心处理细节和请求的传递过程
 * @Author: liu-hao
 * @Date: 2023-04-09 16:48
 * @Version: 1.0
 **/
public class HandlerChain {
    private AbstractHandler firstHandler = null;
    private AbstractHandler lastHandler = null;

    public HandlerChain addHandler(AbstractHandler handler) {
        // 维护继承者
        if (firstHandler == null) {
            firstHandler = handler;
            lastHandler = handler;
        } else {
            lastHandler.setSuccessor(handler);
            lastHandler = handler;
        }
        return this;
    }

    public boolean handle() {
        return firstHandler.handler();
    }
}
